package com.insnow.knowshare.controller;

import com.insnow.knowshare.entity.Post;
import com.insnow.knowshare.entity.PostDTO;
import com.insnow.knowshare.entity.UserActivityDTO;
import com.insnow.knowshare.service.AdminPostService;
import com.insnow.knowshare.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/posts")
public class AdminPostController {
    
    @Autowired
    private AdminPostService adminPostService;

    // 查询所有帖子（已通过）
    @GetMapping
    public Result<?> getAllPosts(
        @RequestParam(value = "page", defaultValue = "0") int page,
        @RequestParam(value = "size", defaultValue = "10") int size
    ) {
        return Result.success(adminPostService.getPostsByStatusPaged(1, page, size));
    }

    // 查询待审核帖子
    @GetMapping("/pending")
    public Result<?> getPendingPosts(
        @RequestParam(value = "page", defaultValue = "0") int page,
        @RequestParam(value = "size", defaultValue = "10") int size
    ) {
        return Result.success(adminPostService.getPostsByStatusPaged(0, page, size));
    }

    // 查询未通过帖子
    @GetMapping("/rejected")
    public Result<?> getRejectedPosts(
        @RequestParam(value = "page", defaultValue = "0") int page,
        @RequestParam(value = "size", defaultValue = "10") int size
    ) {
        return Result.success(adminPostService.getPostsByStatusPaged(2, page, size));
    }

    // 置顶/取消置顶
    @PostMapping("/{id}/top")
    public Result<?> setPostTop(@PathVariable("id") Long id, @RequestBody(required = false) TopRequest req) {
        boolean top = req != null && Boolean.TRUE.equals(req.getTop());
        adminPostService.setPostTop(id, top);
        return Result.success();
    }

    // 删除（软删除）
    @DeleteMapping("/{id}")
    public Result<?> deletePost(@PathVariable("id") Long id) {
        adminPostService.deletePost(id);
        return Result.success();
    }

    // 恢复已删除帖子
    @PostMapping("/{id}/restore")
    public Result<?> restorePost(@PathVariable("id") Long id) {
        adminPostService.restorePost(id);
        return Result.success();
    }

    // 审核通过
    @PostMapping("/{id}/approve")
    public Result<?> approvePost(@PathVariable("id") Long id) {
        adminPostService.updatePostStatus(id, 1);
        return Result.success();
    }

    // 审核拒绝
    @PostMapping("/{id}/reject")
    public Result<?> rejectPost(@PathVariable("id") Long id) {
        adminPostService.updatePostStatus(id, 2);
        return Result.success();
    }

    // DTO
    public static class TopRequest {
        private Boolean top;
        public Boolean getTop() { return top; }
        public void setTop(Boolean top) { this.top = top; }
    }

    @GetMapping("/user-activity")
    public Result<?> getUserActivity(
        @RequestParam("start") String start,
        @RequestParam("end") String end,
        @RequestParam(value = "page", defaultValue = "0") int page,
        @RequestParam(value = "size", defaultValue = "10") int size
    ) {
        LocalDateTime startTime = OffsetDateTime.parse(start).toLocalDateTime();
        LocalDateTime endTime = OffsetDateTime.parse(end).toLocalDateTime();
        return Result.success(adminPostService.getUserActivityPaged(startTime, endTime, page, size));
    }
}
